﻿using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace Sc.Web.Controllers.WebApi
{
    public class UserCarApplicationApiController : ApiController
    {
        private readonly Car_UseCarApplicationService service = new Car_UseCarApplicationService();
        /// <summary>
        /// 用车申请
        /// </summary>
        /// <param name="ent">实体</param>
        /// <returns></returns>
        [HttpPost]
        public object CreateUserCarApplication([FromBody]Car_UseCarApplication ent)
        {
            int tag = 0;
            if (ent.IsApproal == true || ent.IsComplaint == true)
                tag = service.SaveDataForApp(ent);
            else
                tag = service.SaveData(ent);
            return tag;
        }

        /// <summary>
        /// 获取用车申请列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        [HttpPost]
        public object GetPageData()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            int pageSize = Convert.ToInt32(HttpContext.Current.Request["pageSize"]);
            int pageIndex = Convert.ToInt32(HttpContext.Current.Request["pageIndex"]);
            JavaScriptSerializer json = new JavaScriptSerializer();
            var tmp = service.GetList(id, pageSize, pageIndex);
            var data = json.Serialize(tmp);
            if (tmp == null)
            {
                data = "[]";
            }
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 获取用车申请明细
        /// </summary>
        /// <param name="id">业务ID</param>
        /// <returns></returns>
        [HttpPost]
        public object GetModel()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["bid"]);
            var mp = service.GetDetail(id);
            if (mp == null) return "[]";
            JavaScriptSerializer json = new JavaScriptSerializer();
            var data = json.Serialize(mp);
            return new HttpResponseMessage { Content = new StringContent(data, System.Text.Encoding.UTF8, "application/json") }; 
        }
        /// <summary>
        /// 判断当前用户是否具有审核权限
        /// </summary>
        /// <returns>0表示可以审核;1、表示已经审核过</returns>
        [HttpPost]
        public object CurrUserIsApproal()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["bid"]);
            var ent = service.GetDetail(id);
            if (ent.InstanceState == 2)
            {
                return 0;
            }
            int userId = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            return CommService.IsApproval(ent.ApproalUsers, userId, ent.NodeCount);
        }
        /// <summary>
        /// 用车审批
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Approal()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["bid"]);
            int userId = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            int opa = Convert.ToInt32(HttpContext.Current.Request["opa"]);
            int todoId = Convert.ToInt32(HttpContext.Current.Request["todo"]);
            var opContext = HttpContext.Current.Request["opContext"];
            var tmp = service.Approal(id, userId, opa, todoId, opContext);
            JavaScriptSerializer json = new JavaScriptSerializer();
            return new HttpResponseMessage { Content = new StringContent(json.Serialize(tmp), System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 获取用车统计
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object GetStatistics()
        {
            int userId = Convert.ToInt32(HttpContext.Current.Request["userId"]);
            var list = service.GetStatistics(userId);
            JavaScriptSerializer json = new JavaScriptSerializer();
            return new HttpResponseMessage { Content = new StringContent(json.Serialize(list), System.Text.Encoding.UTF8, "application/json") };
        }
        /// <summary>
        /// 取消用车
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object CancelUseApplication()
        {
            int id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            return service.CancelUseApplication(id);
        }
    }
}
